<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Travis CI使用记录 - macdfree&#39;s blog</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="macd" /><meta name="description" content="Travis CI使用记录" />
<meta name="keywords" content="Travis CI" />



<meta name="baidu-site-verification" content="Ak74Sk61bk" />
<meta name="google-site-verification" content="fAeFHpkrhfFS8LxdzXy0EtAPcVStv0faGn238sRF3L8" />


<meta name="generator" content="Hugo 0.54.0" />


<link rel="canonical" href="/posts/travis-ci%E4%BD%BF%E7%94%A8%E8%AE%B0%E5%BD%95/" />

<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">




<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


<link href="/dist/even.min.css?v=3.1.1" rel="stylesheet">
<link href="/lib/fancybox/jquery.fancybox-3.1.20.min.css" rel="stylesheet">




<meta property="og:title" content="Travis CI使用记录" />
<meta property="og:description" content="Travis CI使用记录" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/posts/travis-ci%E4%BD%BF%E7%94%A8%E8%AE%B0%E5%BD%95/" />
<meta property="article:published_time" content="2018-03-15T21:40:29&#43;08:00"/>
<meta property="article:modified_time" content="2018-03-22T22:26:29&#43;08:00"/>

<meta itemprop="name" content="Travis CI使用记录">
<meta itemprop="description" content="Travis CI使用记录">


<meta itemprop="datePublished" content="2018-03-15T21:40:29&#43;08:00" />
<meta itemprop="dateModified" content="2018-03-22T22:26:29&#43;08:00" />
<meta itemprop="wordCount" content="1145">



<meta itemprop="keywords" content="Travis CI," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Travis CI使用记录"/>
<meta name="twitter:description" content="Travis CI使用记录"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">macdfree&#39;s blog</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/archive/">
        <li class="mobile-menu-item">归档</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">macdfree&#39;s blog</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/archive/">归档</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">Travis CI使用记录</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-03-15 </span>
        <div class="post-category">
          
            <a href="/categories/%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90/"> 持续集成 </a>
          
        </div>
        <span class="more-meta"> 约 1145 字 </span>
        <span class="more-meta"> 预计阅读 3 分钟 </span>
        <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
      </div>
    </header>

    
    
<div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
<ul>
<li><a href="#概述">概述</a></li>
<li><a href="#从-travis-yml-说起">从 <code>.travis.yml</code> 说起</a></li>
<li><a href="#项目的构建流程">项目的构建流程</a></li>
</ul>
</nav>
  </div>
</div>

    
    <div class="post-content">
      <p>最近有空使用了一下Travis CI持续集成工具，在此记录一下备忘。</p>

<h1 id="概述">概述</h1>

<p>公司使用的持续集成工具为<strong>Jenkins</strong>，使用Web可视化界面进行配置，操作简单易上手。而 <strong>Travis CI</strong> 通过<strong>yaml</strong>文件（<code>.travis.yml</code>）进行配置，有一点上手难度，但同时提高了灵活度。</p>

<p>从使用下来的感受来说，<strong>Travis CI</strong> 就是根据配置执行一系列脚本，实现代码编译、单元测试、打包和发布的功能。</p>

<h1 id="从-travis-yml-说起">从 <code>.travis.yml</code> 说起</h1>

<p>Travis 要求项目根目录下必须包含 <code>.travis.yml</code> 文件，文件中包含了需要执行操作的配置。从文件后缀名可以看出，使用的是 <strong>yaml</strong> 文件格式，下面看一个 <code>.travis.yml</code> 的示例：</p>

<pre><code class="language-yaml">language: go
sudo: false
go:
  - 1.9.x
deploy:
  provider: releases
  api_key: $GITHUB_OAUTH_TOKEN
  file:
    - &quot;bin/sshs-${TRAVIS_TAG}-linux-amd64&quot;
    - &quot;bin/sshs-${TRAVIS_TAG}-darwin-amd64&quot;
  skip_cleanup: true
  on:
    tags: true
install:
  - go get -d -t -v ./...
script:
  - chmod +x build.sh
  - ./build.sh
notifications:
  email:
    on_failure: always
    on_success: never
</code></pre>

<p>以上是一个使用Go语言的配置，<code>language</code> 配置需要构建的语言环境；<code>go</code> 配置使用的go语言版本；<code>deploy</code> 配置构建好后文件发布的参数，在此不深入展开，具体可参考<a href="https://docs.travis-ci.com/user/deployment/releases/">官方文档</a>；<code>install</code> 配置项目依赖需要执行的指令，可配置多条指令；<code>script</code> 配置构建指令，同样可以配置多条指令；<code>notifications</code> 配置通知方式，具体配置可参考<a href="https://docs.travis-ci.com/user/notifications">官方文档</a>。</p>

<p>对于 <code>.travis.yml</code> 文件，<strong>Travis</strong> 的执行顺序为：<code>before_install</code> -&gt; <code>install</code> -&gt; <code>before_script</code> -&gt; <code>script</code> -&gt; <code>deploy</code>。</p>

<h1 id="项目的构建流程">项目的构建流程</h1>

<p>下图是使用<strong>Travis CI</strong>构建Go项目的输出日志</p>

<p><img src="/images/travislog.png" alt="travislog" /></p>

<p>由图可知，构建任务可分为：</p>

<ol>
<li>拉取<strong>GitHub</strong>仓库代码</li>
<li>设置相关环境变量</li>
<li>将代码同步至指定的目录下（此处应该和具体语言环境相关）</li>
<li>切换至代码所在目录</li>
<li>执行<code>install</code>中的相关指令（如果之前配置了<code>before_install</code>，则先执行<code>before_install</code>中的指令）</li>
<li>执行<code>script</code>中的指令（如果之前配置了<code>before_script</code>，则先执行<code>before_script</code>中的指令）</li>
<li>最后执行发布操作</li>
</ol>

<p>如此梳理过构建流程后，可以发现：<strong>Travis CI</strong>只是提供里构建所需的系统环境和框架，并且开放不同阶段的钩子，具体构建操作可由用户通过<code>.travis.yml</code>自定义配置，提高了可玩性，但同时也对使用者的<strong>bash</strong>脚本编写能力有了一定的要求。</p>

<p>最后说一些题外话，之前本人的博客使用<strong>GitHub Pages + hugo</strong> 搭建。同时原始md文件存放在另一个仓库中，为了实现只要提交md文件，系统自动生成静态页面并发布至<strong>GitHub</strong>的需求，特地使用<strong>Go</strong>编写了一个<strong>BlogCI</strong>的服务程序，<a href="https://github.com/MACDfree/BlogCI">仓库地址</a>，大体思路是：当有md文件提交时，使用<strong>webhooks</strong>发送http请求至<strong>BlogCI</strong>，<strong>BlogCI</strong>接收到请求后在本地拉取最新的md文件，调用<strong>hugo</strong>命令生成静态页面，最后发布至<strong>GitHub Pages</strong>。上述流程看着是不是很眼熟？这不就是<strong>Travis CI</strong>做的事情吗。所以，现在可以摒弃之前丑陋的实现，直接使用<strong>Travis CI</strong>实现博客的自动构建了。详细教程可参考<a href="https://docs.travis-ci.com/user/deployment/pages/">官方文档</a>。</p>
    </div>

    
    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">macd</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">2018-03-22</span>
  </p>
  
  
</div>

    
    
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    
      <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/wechat-qr-code.png">
        <span>微信打赏</span>
      </label>
    
      <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/alipay-qr-code.png">
        <span>支付宝打赏</span>
      </label>
  </div>
</div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
          <a href="/tags/travis-ci/">Travis CI</a>
          
        </div>

      
      <nav class="post-nav">
        
          <a class="prev" href="/posts/axis%E8%B0%83%E7%94%A8cxf%E5%8F%91%E5%B8%83%E7%9A%84webservice%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">Axis调用CXF发布的WebService遇到的问题</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        
          <a class="next" href="/posts/%E5%BF%AB%E9%80%9Fssh%E8%BF%9E%E6%8E%A5%E5%B7%A5%E5%85%B7-%E8%87%AA%E7%94%A8/">
            <span class="next-text nav-default">快速SSH连接工具（自用）</span>
            <span class="prev-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        
  <div id="comments-gitment"></div>
  <link rel="stylesheet" href="/lib/gitment/gitment-0.0.3.min.css">
    <script src="/lib/gitment/gitment-0.0.3.min.js"></script>
  <script type="text/javascript">
  const gitment = new Gitment({
    id: '2018-03-15 21:40:29 \x2b0800 \x2b0800',
    title: 'Travis CI使用记录',
    link: decodeURI(location.href),
    desc: '\x3cp\x3e最近有空使用了一下Travis CI持续集成工具，在此记录一下备忘。\x3c\/p\x3e',
    owner: 'MACDfree',
    repo: 'gitment4',
    oauth: {
      client_id: '3de7eda9fae832753b25',
      client_secret: 'fb1c82c44491295195db0eb7a2d12af22dfcae29'
    }
  })
  gitment.render('comments-gitment')
  </script>
  <noscript>Please enable JavaScript to view the <a href="https://github.com/imsun/gitment">comments powered by gitment.</a></noscript>
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:macdfree@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/MACDfree" class="iconfont icon-github" title="github"></a>
  <a href="/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
    <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    
      2014 - 
    2019
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">macd</span>
    <span class="division">|</span>
    <span style="margin-left: 5px;">
      <img alt="buildstatus" src="https://travis-ci.org/MACDfree/hugoblog.svg?branch=master">
    </span>
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
<script src="/lib/highlight/highlight.pack.js?v=20171001"></script><script type="text/javascript" src="/lib/jquery/jquery-3.2.1.min.js"></script>
  <script type="text/javascript" src="/lib/slideout/slideout-1.0.1.min.js"></script>
  <script type="text/javascript" src="/lib/fancybox/jquery.fancybox-3.1.20.min.js"></script>
<script type="text/javascript" src="/dist/even.min.js?v=3.1.1"></script>








</body>
</html>
